let app = getApp();

Page({
  data: {
    orderNumber:"",
    wuliaotop:"",
    headerData:{},
    bodyData:[],
    wuliao:"",
    num:"1",
    slotModalShow: false,   //弹框是否显示事件
    slotButtonList: [
      {
        name: '关闭',
      }
    ],
    resultList: [
      {
        fenjieItems: [
          { id: 1, selected: false },
          { id: 2, selected: false },
        ]
      },
      {
        orderItem: {}
      },
    ],

  },
  onLoad(options) {
    var that = this
    console.log("接收到的item：", JSON.parse(options.item));
     // 接收pickingLists传过来的数据
     if (options.item) {
      that.setData({
        item: JSON.parse(options.item),
        code: JSON.parse(options.item)['FID']
      })
      console.log("接收pickingLists传过来的数据",that.data.item);
      console.log("接收pickingLists传过来的数据fid",that.data.code);
    }

    that.getPurchaseInquire()
  },


  showImage() {
    this.setData({
      showBigImage: true
    });
  },
  hideImage() {
    this.setData({
      showBigImage: false
    });
  },

  mergeData: function(orderData, fenjieData) {
    let mergedData = [];
    
    // 遍历订单明细数据
    orderData.forEach(function(orderItem) {
      // 遍历分解明细数据
      fenjieData.forEach(function(fenjieItem) {
        // 如果订单明细和分解明细中的物料编码相同，则将它们合并为一个对象
        if (orderItem['物料编码'] === fenjieItem['生产对象']) {
          mergedData.push({
            orderItem: orderItem,
            fenjieItem: fenjieItem
          });
        }
      });
    });
  
    console.log("合并的数据qqqqqqqqqqqq",mergedData);

    // 返回合并后的数据
    return mergedData;

  },


// 订单明细
getPurchaseInquire(e) {
var that = this;
var fidpd = that.data.code
console.log("订单明细数据fid",fidpd);

var data = JSON.stringify(
  {
    "xx": {
      "formId": "ZYKJ_SCRWD",
      "fields": [
        "F_ZYKJ_SCRWD_Entry_FEntryID","F_ZYKJ_BaseProperty","F_ZYKJ_Base1.fnumber","F_ZYKJ_Base_qtr.fname","F_ZYKJ_BaseProperty1"
      ],
      "FilterString": "FID='"+ fidpd +"'",
      "orderby": "",
      "topRowCount": 0,
      "limit": 0,
      "StartRow": 0,
      "fields_name": [
        "实体主键","物料名称","物料编码","品牌","规格型号"
      ],
      "acctID": "66249c7f42e1e4"
    },
    "login": {
      "username": "test",
      "password": "zz333333"
    }
  }
)

// return
dd.httpRequest({
  url: 'http://cloud.reece-sewing.com:9351/king_query',
  method: 'POST',
  headers: {
    "Content-Type": "application/json"
  },
  data: data,
  dataType: 'json',
  success: function (res) {
    if (res.data.code == 200) {
      console.log("订单明细200",res.data);
      var orderMaterialCodeList = res.data.data;
      that.setData({
        wuliaoList: orderMaterialCodeList,
      });

      // 在订单明细获取成功后再获取分解明细数据
      that.getPurchaseFenjie();

    } else {
      dd.showToast({
        type: 'fail',
        content: res.data.data,
        duration: 2000,
      });
    }
  },
  fail: function (res) {
    console.log("订单明细调用失败",res);
  },
  complete: function (res) {
  }
});
},

// 分解明细 过滤只看已完成的
getPurchaseFenjie(e) {
var that = this;
var fidpd = that.data.code
console.log("分解明细数据fid",fidpd);

var data = JSON.stringify(
  {
    "xx": {
      "formId": "ZYKJ_SCRWD",
      "fields": [
        "F_SCRWD_Entry1_FEntryID","F_ZYKJ_BaseProperty4","F_ZYKJ_Base2.fnumber","F_ZYKJ_Base3.fnumber","F_ZYKJ_BaseQty1","F_ZYKJ_Qty_83g","F_ZYKJ_BillStatus_re5","F_ZYKJ_BaseQty_uky","FBillStatus2"
      ],
      "FilterString": "FID='"+ fidpd +"' and F_ZYKJ_BillStatus_re5='1'" ,
      "orderby": "",
      "topRowCount": 0,
      "limit": 0,
      "StartRow": 0,
      "fields_name": [
        "行id","材料名称","生产对象","材料编码","基本单位数量","拣货数量","拣货状态","已配数量","配货状态"
      ],
      "acctID": "66249c7f42e1e4"
    },
    "login": {
      "username": "test",
      "password": "zz333333"
    }
  }
)

dd.httpRequest({
  url: 'http://cloud.reece-sewing.com:9351/king_query',
  method: 'POST',
  headers: {
    "Content-Type": "application/json"
  },
  data: data,
  dataType: 'json',
  success: function (res) {
    if (res.data.code == 200) {
      console.log("分解明细200",res.data);
      var orderMateriafenjieList = res.data.data;

      // 调用mergeData方法将订单明细和分解明细合并
      var mergedData = that.mergeData(that.data.wuliaoList, orderMateriafenjieList);

      // 更新页面数据，展示合并后的数据
      that.setData({
        mergedData: mergedData
      });
      console.log("合并后的数据", that.data.mergedData);

      const items = that.data.mergedData


      // 使用 reduce 方法来将 items 按 orderItem 分组
      const groupedItems = items.reduce((acc, item) => {
        // 使用 orderItem 的实体主键作为唯一标识符
        const key = item.orderItem.实体主键;

        // 如果这个 key 已经存在于累加器中，则添加 fenjieItem 到对应的数组中
        if (acc[key]) {
          acc[key].fenjieItems.push(item.fenjieItem);
        } else {
          // 否则，创建一个新的分组
          acc[key] = {
            orderItem: item.orderItem,
            fenjieItems: [item.fenjieItem]
          };
        }

        return acc;
      }, {});

      // 将 groupedItems 转换为数组格式以便遍历或其他操作
      const result = Object.values(groupedItems);

      // 打印或者以其他方式使用 result
      console.log("这是合并后的对应数据yyyyyyyy",result);

      that.setData({
        resultList: result
      });
      console.log("这是合并后的对应数据data",that.data.resultList);


      // 以下都是为了计算未拣货数量111111111111111111111111111111111111111111111111111111111111111111111111111
      var resultList = that.data.resultList
      // 遍历resultList中的每个订单项和分解项
      resultList.forEach(order => {
        order.fenjieItems.forEach(fenjieItem => {
          // 计算未拣货数量
          fenjieItem.未配货数量 = fenjieItem.拣货数量 - fenjieItem.已配数量;
        });
      });
      // 更新页面数据
      that.setData({
        resultList: resultList
      });
      console.log("这是合并后的对应数据未配货data",that.data.resultList);


    } else {
      dd.showToast({
        type: 'fail',
        content: res.data.data,
        duration: 2000,
      });
    }
  },
  fail: function (res) {
    console.log("分解明细调用失败",res);
  },
  complete: function (res) {
  }
});
},

     // 打开弹框
     tanKuangImage(e){
      console.log("点击了");
      const { item, index } = e.currentTarget.dataset;
      console.log("点击的哪一行啊啊啊啊啊啊啊啊啊啊啊啊",item);
      console.log("点击的哪一行的index",index);
      this.setData({
        slotModalShow: true,
        item,
      });
    },
    // 关闭弹框
    onSlotDmBtnTap() {
      this.setData({
        slotModalShow: false,
          });
    },
  


});
